<template>
  <div class="main">
    <div style="background-color: white">
      <el-form>
        <el-form-item>
          <el-select v-model="bill.type" placeholder="搜索账单类型">
            <el-option
              v-for="item in billTypeList"
              :key="item.billType"
              :value="item.billType"
            >
            </el-option>
          </el-select>

          <el-input v-model="bill.userName" placeholder="搜索记账人">
          </el-input>
          <el-button
            icon="el-icon-search"
            type="primary"
            circle
            @click="getDate()"
          ></el-button>
          <el-button
            style="margin-left: 100px"
            icon="el-icon-refresh-left"
            @click="reset()"
            type="info"
            circle
          ></el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="logDate" label="日期" width="180">
        <template slot-scope="scope">
          <span>{{ dayjs(scope.row.logDate).format("YYYY-MM-DD") }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="amount" label="金额" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.isIncome == 1">+{{ scope.row.amount }}</span>
          <span v-if="scope.row.isIncome == 0">-{{ scope.row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="userName" label="记账人"> </el-table-column>
      <el-table-column prop="billType" label="类型"> </el-table-column>
      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <el-popconfirm
            title="您确定删除这条账单么？"
            cancel-button-text="算了算了"
            @confirm="handleDel(scope.row)"
          >
            <el-button
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              circle
            ></el-button>
          </el-popconfirm>
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="openDialog('edit', scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <pagination
        :total="total"
        :pageNum="pageNum"
        :pageSize="pageSize"
        @changePageSize="changePageSize"
        @changePageNum="changePageNum"
      ></pagination>
    </div>

    <el-drawer
      :title="formTitle"
      size="35%"
      :visible.sync="drawerDialog"
      :direction="direction"
      :before-close="handleClose"
      style="over-flow: scroll"
    >
      <div class="drawerForm" style="margin-left: 60px">
        <el-form
          :model="billForm"
          ref="billForm"
          label-width="130px"
          :rules="rules"
        >
          <!-- <el-form-item label="记账人" prop="userName">
            <el-select
              v-model="billForm.userName"
              filterable
              placeholder="请选择记账人"
            >
              <el-option
                v-for="item in userList"
                :key="item.userName"
                :value="item.userName"
              >
              </el-option>
            </el-select>
          </el-form-item> -->
          <!-- <el-form-item label="个人/团队" prop="isTeam">
            <el-select
              v-model="billForm.isTeam"
              placeholder="请选择个人或团队"
              :change="controlDisable()"
            >
              <el-option
                v-for="item in billOptions"
                :key="item.value"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="收入/支出" prop="isIncome">
            <el-select
              v-model="billForm.isIncome"
              placeholder="请选择收入或支出"
            >
              <el-option
                v-for="item in isIncomeOptions"
                :key="item.value"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账单类型" prop="type">
            <el-select v-model="billForm.billType" placeholder="请选择账单类型">
              <el-option
                v-for="item in billTypeList"
                :key="item.billType"
                :value="item.billType"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账单金额" prop="amount">
            <el-input
              v-model="billForm.amount"
              placeholder="请输入账单金额"
            ></el-input>
          </el-form-item>
          <el-form-item label="团队" prop="teamName">
            <el-select
              v-model="billForm.teamName"
              :disabled="inputDisable"
              placeholder="请选择所属团队"
            >
              <el-option
                v-for="item in teamList"
                :key="item.teamName"
                :value="item.teamName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账单日期" prop="logDate">
            <el-date-picker
              v-model="billForm.logDate"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="备注" prop="amount">
            <el-input
              v-model="billForm.remark"
              placeholder="请输入备注"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitBillForm('billForm')"
              >提交</el-button
            >
            <el-button @click="resetBillForm()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { listPage, add, updateById } from "@/api/bill";
import { listBillType } from "@/api/billType";
import { list } from "@/api/team";
import { listUser } from "@/api/user";
import Pagination from "@/components/Pagination/index";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      role: "",
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 10,
      drawerDialog: false,
      direction: "rtl",
      formTitle: "",
      loading: true,
      bill: {
        minAmount: "",
        maxAmount: "",
        type: "",
        isIncome: "",
        userName: "",
        teamName: "",
        isTeam: "",
        remark: "",
      },
      billForm: {
        id: "",
        userName: "",
        teamName: "",
        amount: "",
        billType: "",
        isIncome: "",
        isTeam: "",
        logDate: "",
        remark: "",
      },
      inputDisable: true,
      teamList: [],
      userList: [],
      rules: {
        userName: [
          { required: true, message: "此选项不能为空", trigger: "blur" },
        ],
        isTeam: [
          { required: true, message: "此选项不能为空", trigger: "blur" },
        ],
        isIncome: [
          { required: true, message: "此选项不能为空", trigger: "blur" },
        ],
        logDate: [
          { required: true, message: "此选项不能为空", trigger: "blur" },
        ],
      },
      billTypeList: [],
      // billOptions: [
      //   {
      //     value: "个人账单",
      //   },
      //   {
      //     value: "团队账单",
      //   },
      // ],
      isIncomeOptions: [
        {
          value: "支出",
        },
        {
          value: "收入",
        },
      ],
    };
  },
  mounted() {
    this.getDate();
    this.getBillTypes();
    this.getTeams();
    this.getUsers();
  },
  methods: {
    changePageSize(val) {
      console.log("页数改变");
      this.pageSize = val;
      this.getDate();
    },
    changePageNum(val) {
      this.pageNum = val;
      this.getDate();
    },
    getDate() {
      const teamName = this.$store.getters.teamName;
      const userName = this.$store.getters.name;
      console.log(teamName, userName);
      if (teamName == "个人版") {
        this.bill.userName = userName;
        this.bill.teamName = teamName;
      } else {
        this.bill.teamName = teamName;
      }
      const param = {
        page: this.pageNum,
        limit: this.pageSize,
        minAmount: this.bill.minAmount,
        maxAmount: this.bill.maxAmount,
        type: this.bill.type,
        isIncome: this.bill.isIncome,
        userName: this.bill.userName,
        teamName: this.bill.teamName,
        isTeam: this.bill.isTeam,
      };
      listPage(param).then((res) => {
        console.log(res.data);
        this.total = res.total;
        this.tableData = res.data;
        this.loading = false;
      });
    },
    getBillTypes() {
      listBillType().then((res) => {
        this.billTypeList = res.data;
      });
    },
    getTeams() {
      list().then((res) => {
        this.teamList = res.data;
      });
    },
    getUsers() {
      listUser().then((res) => {
        this.userList = res.data;
      });
    },
    reset() {
      this.bill = {
        minAmount: "",
        maxAmount: "",
        type: "",
        isIncome: "",
        userName: "",
        teamName: "",
        isTeam: "",
      };
      this.getDate();
    },
    delete() {},
    resetBillForm() {
      this.billForm = {
        id: "",
        userName: "",
        teamName: "",
        amount: "",
        billType: "",
        isIncome: "",
        isTeam: "",
        logDate: "",
        remark: "",
      };
    },
    openDialog(name, data) {
      if (name == "add") {
        this.formTitle = "添加";
      } else if (name == "edit") {
        console.log(data);
        console.log(this.$store.getters.name);
        console.log(data.userName);
        if (
          this.$store.getters.roles[0] == "teamMember" &&
          data.userName != this.$store.getters.name
        ) {
          this.$message({
            showClose: true,
            message: "不是你的账单你无权修改哦！",
            type: "warning",
          });
          return;
        }
        this.formTitle = "编辑";
        if (data.isIncome == 1) {
          this.billForm.isIncome = "收入";
        } else if (data.isIncome == 0) {
          this.billForm.isIncome = "支出";
        }
        if (data.isTeam == 1) {
          this.billForm.isTeam = "团队";
        } else if (data.isTeam == 0) {
          this.billForm.isTeam = "个人";
        }
        this.billForm.id = data.id;
        (this.billForm.userName = data.userName),
          (this.billForm.teamName = data.teamName),
          (this.billForm.amount = data.amount),
          (this.billForm.billType = data.billType),
          (this.billForm.logDate = data.logDate);
        this.billForm.remark = data.remark;
      }
      this.drawerDialog = true;
    },
    handleClose() {
      this.drawerDialog = false;
      this.resetBillForm();
    },
    exchangeIs(isIncome, isTeam) {
      if (isIncome == "支出") {
        this.billForm.isIncome = 0;
      } else {
        this.billForm.isIncome = 1;
      }
      if (isTeam == "团队账单") {
        this.billForm.isTeam = 1;
      } else {
        this.billForm.isTeam = 0;
      }
    },
    controlDisable() {
      if (this.billForm.isTeam == "个人账单") {
        this.billForm.teamName = "";
        this.inputDisable = true;
      } else if (this.billForm.isTeam == "团队账单") {
        this.inputDisable = false;
      }
    },
    submitBillForm(formName) {
      if (this.formTitle == "添加") {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.exchangeIs(this.billForm.isIncome, this.billForm.isTeam);
            add(this.billForm).then((res) => {
              if (res.code == 0) {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.drawerDialog = false;
                this.resetBillForm();
                this.getDate();
              } else {
                this.$message({
                  message: "添加失败",
                  type: "error",
                });
              }
            });
          }
        });
      } else if (this.formTitle == "编辑") {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm("此操作将修改此账单信息, 是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }).then(() => {
              this.exchangeIs(this.billForm.isIncome, this.billForm.isTeam);
              updateById(this.billForm).then((res) => {
                if (res.code == 0) {
                  this.$message({
                    message: "修改成功",
                    type: "success",
                  });
                  this.getDate();
                  this.drawerDialog = false;
                  this.resetBillForm();
                } else {
                  this.$message({
                    message: "修改失败",
                    type: "error",
                  });
                }
              });
            });
          }
        });
      }
    },
    handleDel(param) {
      console.log(this.$store.getters.name);
      console.log(param.userName);
      if (
        this.$store.getters.roles[0] == "teamMember" &&
        param.userName != this.$store.getters.name
      ) {
        this.$message({
          showClose: true,
          message: "不是你的账单你无权删除哦！",
          type: "warning",
        });
        return;
      }
      const bill = {
        id: param.id,
        isDeleted: 1,
      };
      updateById(bill).then((res) => {
        if (res.code == 0) {
          this.$message({
            showClose: true,
            message: "删除成功",
            type: "success",
          });
          this.getDate();
        } else {
          this.$message({
            showClose: true,
            message: "删除失败",
            type: "erorr",
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-input {
  width: 150px;
  padding: 10px;
}
.el-select {
  width: 200px;
  padding: 10px;
}
.el-table-column {
  text-align: center;
}
.main {
  // margin-left: 30px;
}
.pagination {
  margin-top: 30px;
  text-align: center;
}
.el-input {
  width: 200px;
}
.el-textarea {
  width: 200px;
}
.el-form-item {
  margin-bottom: 10px;
}
</style>
